package light.head.controller.el;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import light.head.constant.Msg;
import light.head.constant.Parm;
import light.head.constant.Url;
import light.head.dao.el.PaperTemplateDAO;
import light.head.dao.el.PaperTemplateItemDAO;
import light.head.enm.RESULT;
import light.head.framework.base.SupporterController;
import light.head.framework.entity.PageBean;
import light.head.model.el.PaperTemplate;
import light.head.model.el.PaperTemplateItem;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

@Controller
@RequestMapping(value = Url.C_TEMP)
public class PaperTemplateCotroller extends SupporterController{
	private PaperTemplateDAO tempDAO;
	private PaperTemplateItemDAO itemDAO;
	
	
	@RequestMapping(Url.INIT)
	public ModelAndView init(HttpServletRequest request, HttpServletResponse response){
		return new ModelAndView(Url.TEMP_INIT);
	}
	
	@RequestMapping(Url.QUERY)
	public ModelAndView query(HttpServletRequest request, HttpServletResponse response){
		PageBean pb = createPageBean(request);
		Map<String, String> map = createQueryMapByParmName(request, Parm.NAME, Parm.DELETED);
		tempDAO.findByMap(cu_organCode(request), map, pb);
		request.setAttribute(Parm.PAGE_BEAN, pb);
		return new ModelAndView(Url.TEMP_LIST);
	}
	
	@RequestMapping(Url.EDIT)
	public ModelAndView edit(HttpServletRequest request, HttpServletResponse response){
		String code = request.getParameter(Parm.CODE);
		if(isNotNull(code)){
			PaperTemplate temp = tempDAO.get(Integer.parseInt(code));
			List<PaperTemplateItem> list = itemDAO.findByTtc(temp.getCode());
			temp.setList(list);
			request.setAttribute("temp", temp);
		}
		request.setAttribute("oc", cu_organCode(request));
		return new ModelAndView(Url.TEMP_EDIT);
	}
	
	
	@RequestMapping(Url.SAVE)
	@Transactional(rollbackFor = Exception.class)
	public void save(PaperTemplate temp, HttpServletRequest request, HttpServletResponse response) throws Exception{
		RESULT result = RESULT.ERROR;
		if(temp.getCode() == null){//新增
			Date ct = new Date();
			temp.setCu(cu_code(request));
			temp.setOc(cu_organCode(request));
			temp.setCt(ct);
			temp.setDeleted(false);
			if(tempDAO.add(temp) == RESULT.SUCCESS){
				List<PaperTemplateItem> itemList = temp.getList();
				for(PaperTemplateItem item : itemList){
					if(item.getLc() != null){
						item.setTtc(temp.getCode());
						item.setDeleted(false);
						if(itemDAO.add(item) != RESULT.SUCCESS){
							throw new RuntimeException("操作失败! ");
						}
					}
				}
				result = RESULT.SUCCESS;
			}
		}else{//修改
			if(tempDAO.update(temp) == RESULT.SUCCESS){
				List<PaperTemplateItem> oldList = itemDAO.findByTtc(temp.getCode());
				List<PaperTemplateItem> itemList = temp.getList();
				List<PaperTemplateItem> list = new ArrayList<PaperTemplateItem>();
				for(PaperTemplateItem item : itemList){
					if(item != null && item.getCode() != null){
						for(PaperTemplateItem old : oldList){
							if(old.getCode().intValue() == item.getCode().intValue()){
								list.add(old);
							}
						}
					}
				}
				//删除
				for(PaperTemplateItem rem : list){
					oldList.remove(rem);
				}
				for(PaperTemplateItem del : oldList){
					del.setDeleted(true);
					tempDAO.upd(del);
				}
				//修改
				for(PaperTemplateItem upd : itemList){
					if(upd != null && upd.getNum() != null && upd.getPoint() != null && upd.getCode() != null){
						itemDAO.update(upd);
					}
				}
				//增加
				for(PaperTemplateItem add : itemList){
					if(add != null && add.getNum() != null && add.getPoint() != null && add.getCode() == null){
						add.setTtc(temp.getCode());
						add.setDeleted(false);
						itemDAO.add(add);
					}
				}
				result = RESULT.SUCCESS;
			}
		}
		
		if(RESULT.SUCCESS == result){
			tempDAO.loadAll();
			print(response, Msg.SUCCESS);
		}else{
			print(response, Msg.ERROR);
		}
	}

	//屏蔽
	@RequestMapping(Url.SHIELD)
	@Transactional(rollbackFor = Exception.class)
	public void shield(HttpServletRequest request, HttpServletResponse response) throws Exception{
		String code = request.getParameter(Parm.CODE);
		RESULT result = RESULT.ERROR;
		if(isNotNull(code)){
			PaperTemplate temp = tempDAO.get(Integer.parseInt(code));
			if(temp != null){
				temp.setDeleted(true);
				result = tempDAO.upd(temp);
			}
		}
		if(RESULT.SUCCESS == result){
			tempDAO.loadAll();
			print(response, Msg.SUCCESS);
		}else{
			print(response, Msg.ERROR);
		}
	}
	
	//解除屏蔽
	@RequestMapping(Url.UN_SHIELD)
	@Transactional(rollbackFor = Exception.class)
	public void unshield(HttpServletRequest request, HttpServletResponse response) throws Exception{
		String code = request.getParameter(Parm.CODE);
		RESULT result = RESULT.ERROR;
		if(isNotNull(code)){
			PaperTemplate temp = tempDAO.get(Integer.parseInt(code));
			if(temp != null){
				temp.setDeleted(false);
				result = tempDAO.upd(temp);
			}
		}
		if(RESULT.SUCCESS == result){
			tempDAO.loadAll();
			print(response, Msg.SUCCESS);
		}else{
			print(response, Msg.ERROR);
		}
	}
	
	
	
	@Autowired
	public void setTempDAO(PaperTemplateDAO tempDAO) {
		this.tempDAO = tempDAO;
	}
	@Autowired
	public void setItemDAO(PaperTemplateItemDAO itemDAO) {
		this.itemDAO = itemDAO;
	}
	
}
